import { Minder }  from '../Minder'

var navigator={
	init() {
		console.log("navigator init");
		$.ajax({
			url : "http://localhost:8081/tree/view?name=菜单配置",
			type : "GET",
			dataType: "json",
			success : function(data){
				console.log("success:"+(JSON.stringify(data)));
				var contaner = $("#nav-menu-content");
				(function refreshMenu(data,level,c) {
					var parentId = data.parentId;
					var text = data.text;
					var id = data.id;
					if(level == 1) {
						// $(".left-title a").html(text);
					}
					else if(level == 2) {
						c.append('<div id="item-'+id+'" class="nav-menu"><div class="nav-title">'+text+'</div><div class="nav-content"></div></div>');
					}
					else if(level == 3) {
						// console.log(parentId+" item level3 add "+text);
						var selectName = '#item-'+parentId+' .nav-content';
						// console.log(parentId+" item :"+c.html());
						c.find(selectName).append('<a class="menu-item" value="'+text+'">'+text+'</a>');
					}
					else {
						return ;
					}
					data.children&&data.children.forEach((d)=>{
						refreshMenu(d,level+1,c);
					})
				 })(data,1,contaner);
				 
				 $('.nav-title').click(function() {
				     $(this).siblings('.nav-content').stop().toggle(500).parent().siblings('.nav-menu').children('.nav-content').hide(500);
				 });
				 $('.menu-item').click(function(){
				     var value=$(this).attr('value');
					 var currentEditing = window.m.getRoot().getText();
					 if(currentEditing == value)
						return ;
				 	console.log(value+" clicked");
				 	$.ajax({
				 		url : "http://localhost:8081/tree/view?name="+value,
				 		type : "GET",
				 		dataType: "json",
				 		success : function(data){
							data.text = data.text||value;
				 			console.log("success:"+(JSON.stringify(data)));
				 			// this.refreshMinder(data);
				 			data.textFill = '#fff';
				 			data.textPadding = [15,20,15,20];
				 			data.fontSize = 18;
				 			data.textBackgroundColor = '#00aaff';
				 			data.textBorderRadius = 4;
				 			data.textOffset = [10,6];
				 			
				 			JSON.stringify("data:"+data)
				 			// var minder=new Minder(data);
				 			
				 			window.m.update1(data);
				 			// window.m.setData(data);
				 			// window.m.initNodes();
				 		},
				 		error : function(xhr,status,error) {
				 			alert("request server failure!");
				 			console.log("error:"+status);
				 		}
				 	});
				 });
			},
			error : function(xhr,status,error) {
				alert("request server failure!");
				console.log("error:"+status);
			}
		});
		
		$('.nav-title').click(function() {
		    $(this).siblings('.nav-content').stop().toggle(500).parent().siblings('.nav-menu').children('.nav-content').hide(500);
		});
	}
};
export default navigator;